package com.able.wanandroid.module.tree;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.able.wanandroid.R;
import com.able.wanandroid.app.Constants;
import com.able.wanandroid.base.BaseFragment;
import com.able.wanandroid.bean.Resource;
import com.able.wanandroid.bean.TreeBean;
import com.able.wanandroid.databinding.FragmentTreeBinding;
import com.able.wanandroid.utils.GsonUtils;
import com.able.wanandroid.utils.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 知识体系
 *
 * @author Able
 * @date 2018/3/20
 */
public class TreeFragment extends BaseFragment<FragmentTreeBinding> implements
        SwipeRefreshLayout.OnRefreshListener {

    private ArrayList<TreeBean> mTreeList;
    private TreeAdapter mTreeAdapter;
    private TreeViewModel treeViewModel;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tree;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initView();
        onRefresh();
    }

    private void initView() {
        mBinding.swipeRefreshLayout.setColorSchemeResources(
                android.R.color.holo_blue_light,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        mBinding.swipeRefreshLayout.setOnRefreshListener(this);

        initAdapter();
    }

    private void initAdapter() {
        mTreeList = new ArrayList<>();
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mTreeAdapter = new TreeAdapter(R.layout.item_tree_list, mTreeList);
        mTreeAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        mBinding.recyclerView.setAdapter(mTreeAdapter);

        mTreeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ToastUtils.showInfo(getContext(), position + "");

                Intent intent = new Intent(getContext(), TreeArticleActivity.class);
                intent.putExtra(Constants.EXTRA_TREE_NAME, mTreeList.get(position).getName());
                intent.putExtra(Constants.EXTRA_TREE_CHILDREN, GsonUtils.toJson(mTreeList.get(position).getChildren()));
                startActivity(intent);
            }
        });
    }

    @Override
    public void onRefresh() {
        treeViewModel = ViewModelProviders.of(TreeFragment.this).get(TreeViewModel.class);
        treeViewModel.getTreeData().observe(TreeFragment.this, new Observer<Resource<List<TreeBean>>>() {
            @Override
            public void onChanged(@Nullable Resource<List<TreeBean>> listResource) {
                List<TreeBean> data = listResource.data;
                mTreeList.clear();
                mTreeList.addAll(data);
                mTreeAdapter.notifyDataSetChanged();
                mBinding.swipeRefreshLayout.setRefreshing(false);
            }
        });
    }
}
